#include "Triangle.h"

Triangle::Triangle()
{
  _normal[0] = 0;
  _normal[1] = 0;
  _normal[2] = 0;
}

Triangle::Triangle (const Triangle& t)
{
  _verts[0] = t.vertex(0);
  _verts[1] = t.vertex(1);
  _verts[2] = t.vertex(2);
}

Triangle::Triangle
(Vertex3D** vertex)
{
  _verts[0] = *vertex;
  _verts[1] = *(vertex + 1);
  _verts[2] = *(vertex + 2);
}

Triangle::~Triangle () {}

Vertex3D*
Triangle::vertex (int index) const 
{
	return _verts[index];
}

void
Triangle::compute_normal ()
{
	GLdouble av[] = {_verts[1]->x () - _verts[0]->x (), _verts[1]->y () - _verts[0]-> y(), _verts[1]->z () - _verts[0]->z ()};
	GLdouble bv[] = {_verts[2]->x () - _verts[0]->x (), _verts[2]->y () - _verts[0]-> y(), _verts[2]->z () - _verts[0]->z ()};
	Vertex3D a (av), b (bv);
  	a.vec3_prod (b, _normal);
	normalize (_normal);
}

GLdouble*
Triangle::normal () 
{
	return _normal;
}

Vertex3D
Triangle::normalv () 
{
	return Vertex3D(_normal);
}

bool
Triangle::adyacent(Triangle *t)
{
	bool res=false;
	if(((_verts[0]==(t->vertex(1))) && (_verts[1]==(t->vertex(0)))) || ((_verts[0]==(t->vertex(0))) && (_verts[1]==(t->vertex(2)))) || ((_verts[0]==(t->vertex(1))) && (_verts[1]==(t->vertex(2)))) || ((_verts[0]==(t->vertex(2))) && (_verts[1]==(t->vertex(0)))) || ((_verts[0]==(t->vertex(0))) && (_verts[1]==(t->vertex(1)))) || ((_verts[2]==(t->vertex(2))) && (_verts[1]==(t->vertex(0)))) || ((_verts[2]==(t->vertex(1))) && (_verts[1]==(t->vertex(2)))) || ((_verts[2]==(t->vertex(0))) && (_verts[1]==(t->vertex(2)))) || ((_verts[2]==(t->vertex(1))) && (_verts[1]==(t->vertex(0)))) || ((_verts[2]==(t->vertex(2))) && (_verts[1]==(t->vertex(1)))) || ((_verts[2]==(t->vertex(1))) && (_verts[0]==(t->vertex(2)))) || ((_verts[2]==(t->vertex(0))) && (_verts[0]==(t->vertex(1)))) || ((_verts[2]==(t->vertex(1))) && (_verts[0]==(t->vertex(0)))) || ((_verts[2]==(t->vertex(2))) && (_verts[0]==(t->vertex(1)))) || ((_verts[2]==(t->vertex(0))) && (_verts[0]==(t->vertex(2)))))
	    res=true;
	return res;
}
